#ifndef T_BOX_H
#define T_BOX_H

#include "stable.h"

#define USE_DECODE_THREAD 0
//#define USE_DECODE_THREAD 1

class T_Box
{
public:
    enum
    {
        PLAYER_HEAD_HEADGE = 2000,
        PLAYER_BUFFER_TIME = 6000,
        PLAYER_AHEAD_IMAGE = 5, PLAYER_ENOUGH_IMAGE = 10, PLAYER_IMAGE_STOCK = 20,
        //PLAYER_AHEAD_IMAGE = 1, PLAYER_ENOUGH_IMAGE = 1, PLAYER_IMAGE_STOCK = 2,
        //PLAYER_AHEAD_IMAGE = 15,
        //PLAYER_IMAGE_STOCK = 60,
        //PLAYER_IMAGE_STOCK = 120,
        //PLAYER_IMAGE_STOCK = 240,
    };
    explicit T_Box();
};

#if 0x0
class T_Box_Thread : public QThread
{
public:
    explicit T_Box_Thread()
    {
        closing(false);
    }
    void finish()
    {
        closing(true);
        this->p_wait_closing();
    }
    bool closing()
    {
        return m_closing;
    }
protected:
    void closing(bool b)
    {
        m_closing = b;
    }
    void p_wait_closing()
    {
        if(qApp->thread() != QThread::currentThread())
        {
            this->wait();
            return;
        }
        while(this->isRunning())
        {
            qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
            //msleep(1);
        }
    }
private:
    bool m_closing;
};
#else
class T_Box_Thread : public QThread
{
public:
    explicit T_Box_Thread(QObject *parent = 0)
        : QThread(parent)
    {
        this->p_reset_status();
    }
    void start()
    {
        this->p_reset_status();
        QThread::start();
        this->p_wait_ready();
    }
    void finish()
    {
        QThread::quit();
        closing() = true;
        this->p_wait_closing();
        this->p_reset_status();
    }
protected:
    bool &ready()
    {
        return m_ready;
    }
    bool &closing()
    {
        return m_closing;
    }
    void p_reset_status()
    {
        ready() = false;
        closing() = false;
    }
    void p_wait_ready()
    {
        while(!ready())
        {
            if(qApp->thread() == QThread::currentThread())
            {
                qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
            }
            else
            {
                msleep(1);
            }
        }
    }
    void p_wait_closing()
    {
        if(qApp->thread() != QThread::currentThread())
        {
            this->wait();
            return;
        }
        while(this->isRunning())
        {
            qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
        }
    }
private:
    bool m_ready;
    bool m_closing;
};
#endif

#endif // T_BOX_H
